package org.ahuanlien.crm.service.impl;

import org.ahuanlien.crm.domain.ClientRepaymentInfo;
import org.ahuanlien.crm.mapper.ClientRepaymentInfoMapper;
import org.ahuanlien.crm.query.ClaimChartQueryObject;
import org.ahuanlien.crm.query.ClientRepaymentInfoQueryObject;
import org.ahuanlien.crm.service.IClientRepaymentInfoService;
import org.ahuanlien.crm.util.LogicException;
import org.ahuanlien.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class ClientRepaymentInfoServiceImpl implements IClientRepaymentInfoService {

    @Autowired
    ClientRepaymentInfoMapper clientRepaymentInfoMapper;

    /*插入新的通知单*/
    public void insert(ClientRepaymentInfo record) {
        clientRepaymentInfoMapper.insert(record);
    }

    /*查询界面*/
    public PageResult query(ClientRepaymentInfoQueryObject qo) {
        int count = clientRepaymentInfoMapper.query4Count(qo);
        if (count == 0) {
            return new PageResult(count, Collections.EMPTY_LIST);
        }
        List<ClientRepaymentInfo> result = clientRepaymentInfoMapper.query4list(qo);
        return new PageResult(count, result);
    }

    /*通知客户 , 改变状态*/
    public void informClient(String repaymentInfoNumber) {
        ClientRepaymentInfo clientRepaymentInfo = clientRepaymentInfoMapper.queryByInfoSn(repaymentInfoNumber);
        if (clientRepaymentInfo.getStatus()==1) {
            throw new LogicException("当前通知单已通知客户");
        }
        clientRepaymentInfoMapper.informClient(repaymentInfoNumber);
    }

    public ClientRepaymentInfo queryByInfoSn(String infoSn) {
        return clientRepaymentInfoMapper.queryByInfoSn(infoSn);
    }

    public List<Map<String, BigDecimal>> queryByGroupType(ClaimChartQueryObject qo) {
        return clientRepaymentInfoMapper.queryByGroupType(qo);
    }


    /*插入意见*/
    public void setOpinion(String opinion, String infoSn) {
        ClientRepaymentInfo clientRepaymentInfo = clientRepaymentInfoMapper.queryByInfoSn(infoSn);
        if (clientRepaymentInfo.getStatus()==1) {
            throw new LogicException("当前通知单已通知客户,不可以再添加意见");
        }
        clientRepaymentInfoMapper.setOpinion(opinion , infoSn);
    }
}
